import java.util.*;


/**
 * @Author: Lenovo
 * @Date: 2024/8/3 20:10
 * @description:
 */
public class Test {
    public List<String> topKFrequent(String[] words, int k) {
        //1.遍历words
        HashMap<String,Integer> map = new HashMap<>();
        for (String word:words){
            if (!map.containsKey(word)){
                map.put(word,1);
            }else {
                int val = map.get(word);
                map.put(word,val+1);
            }
        }
        // 2.第二步创建小根堆
        PriorityQueue<Map.Entry<String,Integer>>minHeap = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o2.getValue().compareTo(o1.getValue())== 0){
                    return  o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        //3.遍历map
        for(Map.Entry<String,Integer> entry:map.entrySet()){
            if (minHeap.size() < k){
                minHeap.offer(entry);
            }else {
                Map.Entry<String,Integer> top = minHeap.peek();
                if (top.getValue().compareTo(entry.getValue()) < 0){
                    minHeap.poll();
                    minHeap.offer(entry);
                }else if (top.getValue().compareTo(entry.getValue()) == 0) {
                    if (top.getKey().compareTo(entry.getKey())>0){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        List<String> list = new ArrayList<>();
        for(int i = 0;i<k;i++){
            Map.Entry<String,Integer> entry = minHeap.poll();
            list.add(entry.getKey());
        }
        Collections.reverse(list);
        return list;
    }
    public int numJewelsInStones(String jewels, String stones) {
        HashSet<Character> set = new HashSet();
        int cnt = 0;
        for (int  i = 0;i<jewels.length();i++){
            char ch = jewels.charAt(i);
            set.add(ch);
        }
        for (int j = 0 ;j<stones.length();j++){
            char ch = stones.charAt(j);
            if (set.contains(ch)){
                cnt++;
            }
        }
        return cnt;
    }
     static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    public Node copyRandomList(Node head) {
        HashMap<Node,Node> map = new HashMap<>();
        Node cur = head;
        while(cur != null){
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }
        cur = head;
        while (cur != null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
    public int singleNumber(int[] nums) {
        HashSet<Integer> set = new HashSet();
        for (int i = 0;i<nums.length;i++){
            if (!set.contains(nums[i])){
                set.add(nums[i]);
            }else{
                set.remove(nums[i]);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                return nums[i];
            }
        }
            return -1;
        }

    public static void main(String[] args) {
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("xxhh",123);
        hashMap.get("xxhh");
    }
}
